package databaseVersionControl.domain.control;

import databaseVersionControl.infra.annotation.DatabaseScript;
import databaseVersionControl.infra.script.validation.ScriptValidation;

public class ScriptKey extends AbstractScriptKey {
	
	public ScriptKey(Class<?> clazz, int number) {
		validate(clazz, number);
		
		setDatabaseScriptKey(getDatabaseScriptKeyForClass(clazz));
		setNumber(number);
	}

	public static String getDatabaseScriptKeyForClass(Class<?> clazz) {
		return clazz.getAnnotation(DatabaseScript.class).name();
	}
	
	private void validate(Class<?> clazz, Integer number) {
		if ( !ScriptValidation.isScriptClass(clazz) )
			throw new RuntimeException("Class have to be a DatabaseScript");
			
		if ( !ScriptValidation.isValidNumber(clazz, number) )
			throw new RuntimeException(String.format("Number %d is not present in DatabaseScript %s", number, clazz.getSimpleName()));
	}
	
}